home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / corewars / tokenize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-14  |  4.1 KB  |  168 lines

  1. /*    Copyrighted (C) 1989 by Na Choon Piaw.  All rights reserved       */
  2.  
  3.  
  4.  
  5. /*    This program and documentation is Public Domain, and may be      */
  6. /*    distributed and copied by everyone provided this header          */
  7. /*    remains intact                              */
  8.  
  9. /* tokenize ---- tokenizer for the assembler.  It splits up all of the
  10.    tokens in the given input file, generate a linked list of such tokens,
  11.    and outputs the pointer to that linked list.
  12.   11/14                 ----- NCP                                    */
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <strings.h>
  16. #include <malloc.h>
  17. #include "assem.h"
  18.  
  19. /* tokenize function:
  20.    separate input stream (infile) into tokens.
  21.    algorithm:
  22.     1)    call nextoken to get next token.
  23.     2)    if "null" then return the first pointer
  24.     3)    otherwise, add the newtoken to the token list
  25.     4)    go back to 1.
  26. */
  27. tokenlist *tokenize(infile)
  28. FILE    *infile;
  29. {
  30.     tokenlist    *head, *tail, *newtail;
  31.     char        *nextoken(), *newtoken;
  32.  
  33.     head = tail = NULL;
  34.     while ((newtoken = nextoken(infile)) != NULL)
  35.     {
  36.         if (!(newtail = (tokenlist *) 
  37.                          malloc((unsigned) sizeof(tokenlist))))
  38.         {
  39.             printf("ran out of space for tokens: %s\n", newtoken);
  40.             printf("------  tokenize\n");
  41.             exit(1);
  42.         }
  43.  
  44.         /* otherwise , set old stuff to this and move tail one up*/
  45.         newtail -> token = newtoken;
  46.         newtail -> next = NULL;
  47.         if (tail)            /* tail already defined */
  48.         {
  49.             tail -> next = newtail;        /* set previous ptr */
  50.             tail = tail -> next;        /* move up list */
  51.         }
  52.         else
  53.             head = tail = newtail;
  54.     }    /* end while */
  55.  
  56.     return (head);    /* return function value */
  57. }
  58.  
  59. /* function next token:
  60.    return next token in the file.
  61.    Algorithm:
  62.     1)    read until start of next token or EOF
  63.     2)    if EOF then return NULL pointer
  64.     3)    read new token into buffer
  65.     4)    malloc new string
  66.     5)    put next token into new string
  67.     6)    change all characters into upper case
  68.     7)    return pointer to string created in (4)      */
  69. /* BUG NOTE:
  70.    Due to the way it is written, the assembler will not process things like
  71.    "mov0 1" correctly, and neither will "mov 0 1;imp" work.
  72.    this is because the tokenize breaks everything down that isn't a
  73.    delimiter, and a ";" is not a delimiter, even though it's not in
  74.    the instruction set.                                   NCP - 11/15/88 */
  75. char    *nextoken(infile)
  76. FILE    *infile;
  77. {
  78.     char    buffer[MAXBUFFER],    /* string buffer */
  79.         *newtoken;        /* new token pointer */
  80.  
  81.     int    c,            /* character we read in one by one */
  82.         i = 0;            /* integer counter */
  83.  
  84.     while ((c = fgetc(infile)) != EOF)
  85.     {
  86.         if (!isspace(c))
  87.             break;        /* not space, so process */
  88.     }
  89.  
  90.     if (c == EOF)
  91.         return(NULL);        /* no more!! */
  92.  
  93.     if (c == COMMENT)        /* handle comments */
  94.     {
  95.         while (((c = fgetc(infile)) != '\n') && c != EOF)
  96.             ;        /* read until end of the line */
  97.  
  98.         if (c == EOF)
  99.             return(NULL);
  100.  
  101.         /* process the rest of the file:
  102.            actually, we could have done this by using a goto
  103.            the beginning of this function, but I think this
  104.            is a lot more elegant                     --- CP */
  105.         return(nextoken(infile));
  106.     }
  107.  
  108.     while ((!isspace(c)) && (c != EOF))    /* read until next space */
  109.     {
  110.         if (i >= MAXBUFFER)    /* buffer out */
  111.         {
  112.             printf("buffer over extended\n");    
  113.             printf("--- nextoken\n");
  114.             exit(1);
  115.         }
  116.  
  117.         buffer[i++] = c;
  118.  
  119.         c = fgetc(infile);
  120.     }    /* end while */
  121.     buffer[i] = '\0';        /* terminate with a null */
  122.  
  123.     if (!(newtoken = (char *) malloc( (unsigned) strlen(buffer) + 1)))
  124.     {
  125.         printf("not enough memory for token %s\n", buffer);
  126.         printf(" ------- nextoken\n");
  127.         exit(1);
  128.     }
  129.  
  130.     strcpy(newtoken, buffer);
  131.  
  132.     upcase(newtoken);
  133.  
  134.     return(newtoken);
  135. }    /* end of nextoken */
  136.  
  137. /* upcase function -- translate string to upper case (don't trust toupper) */
  138. upcase(str)
  139. char    *str;
  140. {
  141.     while (*str)
  142.     {
  143.         if ((*str <= 'z') && (*str >= 'a'))
  144.             *str -= 'a' - 'A';
  145.         str++;
  146.     }
  147. }
  148.  
  149. /* special main to test the above */
  150. /* section commented out because code has been fully debugged
  151.    and given a clean bill of health            CP - 11/14/88 
  152. main(argc,argv)
  153. int    argc;
  154. char    *argv[];
  155. {
  156.     FILE    *f;
  157.     tokenlist    *head;
  158.  
  159.     f = fopen(argv[1],"r");
  160.     head = tokenize(f);
  161.     while (head != NULL)
  162.     {
  163.         printf("%s\n", head -> token);
  164.         head = head -> next;
  165.     }
  166. } */
  167.  
  168.